package cn.toma.service.impl.sys;

import cn.toma.core.AbstractService;
import cn.toma.dao.sys.RoleMenuMapper;
import cn.toma.model.sys.RoleMenu;
import cn.toma.service.sys.RoleMenuServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service("roleMenuService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RoleMenuServiceImpl extends AbstractService<RoleMenu> implements RoleMenuServie
{

	@Resource
	private RoleMenuMapper roleMenuMapper;

	@Override
	@Transactional
	public void deleteRoleMenusByRoleId(String[] roleIds)
	{
		List<String> list = Arrays.asList(roleIds);
		Weekend<RoleMenu> weekend = new Weekend<>(RoleMenu.class);
		WeekendCriteria<RoleMenu, Object> weekendCriteria = weekend.weekendCriteria();
		weekendCriteria.andIn(RoleMenu::getRoleId, list);
		this.roleMenuMapper.deleteByCondition(weekend);

	}

	@Override
	@Transactional
	public void deleteRoleMenusByMenuId(String[] menuIds)
	{
		List<String> list = Arrays.asList(menuIds);

		Weekend<RoleMenu> weekend = new Weekend<>(RoleMenu.class);
		WeekendCriteria<RoleMenu, Object> weekendCriteria = weekend.weekendCriteria();
		weekendCriteria.andIn(RoleMenu::getMenuId, list);
		this.roleMenuMapper.deleteByCondition(weekend);
	}

	@Override
	public List<RoleMenu> getRoleMenusByRoleId(String roleId)
	{
		Weekend<RoleMenu> weekend = new Weekend<>(RoleMenu.class);
		WeekendCriteria<RoleMenu, Object> weekendCriteria = weekend.weekendCriteria();
		weekendCriteria.andEqualTo(RoleMenu::getRoleId, roleId);
		List<RoleMenu> roleMenus = this.roleMenuMapper.selectByCondition(weekend);
		return roleMenus;
	}

}
